package com.mce.nebula.config;

import com.mce.nebula.service.ConfigService;
import com.mce.nebula.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 数据初始化器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DataInitializer implements CommandLineRunner {

    private final UserService userService;
    private final ConfigService configService;

    @Value("${app.enterprise.default-id}")
    private String defaultEnterpriseId;

    @Override
    public void run(String... args) throws Exception {
        initDefaultAdminUser();
    }

    /**
     * 初始化默认管理员用户
     */
    private void initDefaultAdminUser() {
        try {

            if (this.configService.count() == 0) {

            }

            // 检查是否已存在管理员用户
            if (!userService.userExists("admin")) {
                userService.createUser(
                        "admin",
                        "admin@rainbond.com",
                        "123456",
                        defaultEnterpriseId
                );
                log.info("Default admin user created: admin/123456");
            } else {
                log.info("Admin user already exists");
            }
        } catch (Exception e) {
            log.error("Failed to create default admin user", e);
        }
    }
}





